﻿using Prism.Ioc;
using Prism.Regions;
using Prism.Services.Dialogs;
using System.Windows;

namespace PrismDemo.Views
{

    public partial class MainWindow : Window
    {
        private readonly IRegionManager regionManager;
        private readonly IContainerExtension container;
        private readonly IDialogService dialogService;
        //后台注入IRegionManager
        public MainWindow(IRegionManager regionManager, IContainerExtension containerExtension, IDialogService dialogService)
        {
            InitializeComponent();
            this.regionManager = regionManager;
            this.container = containerExtension;
            this.dialogService = dialogService;
            RegionManager.SetRegionName(chartRegion, "chartRegion");
            this.Loaded += MainWindow_Loaded;
        }
        //因为构造函数运行完之前RegionName还没创建完成，区域Regions通过名称找不到
        //所以要在Loded中创建而不是在构造函数中
        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            //【1】直接在主窗口的构造函数中注入页面并关联Region
            ShowModel showModel = container.Resolve<ShowModel>();
            //Add关联页面；Deactivate休眠页面；Activate激活页面；Remove取消关联页面
            regionManager.Regions["modelRegion"].Add(showModel);
            //【2】也可以在App.xaml.cs中全局注册导航页面然后主窗口中关联页面
            //containerRegistry.RegisterForNavigation<ChartTest>();
            regionManager.Regions["chartRegion"].RequestNavigate("ChartTest");
        }

        private void Button_Dialog(object sender, RoutedEventArgs e)
        {
            //传入的参数是我们在App.cs中注册的Dialog窗口名字
            IDialogParameters parameters = new DialogParameters();
            //这个参数是从当前窗口传递口要打开的子窗口的参数，不要和下面回调函数中的参数搞混了
            //此para由弹窗的ViewModel中的OnDialogOpened方法接收
            parameters.Add("param1", "父传子参数");
            this.dialogService.ShowDialog("DialogTest", parameters, DialogCallback);
        }
        private void DialogCallback(IDialogResult result)
        {
            //通过事件关闭子窗口后会触发此方法
            //这个参数是子窗口关闭时候，子窗口传递过来的
            ButtonResult result1 = result.Result;
            var param = result.Parameters.GetValue<string>("param2");
            MessageBox.Show(param);
        }
    }
}
